﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;

namespace Connect
{
    /// <summary>
    /// Lưu dữ thông tin kết nối tới cơ sở dữ liệu
    /// </summary>
    public class Connection
    {
        public string Server { get; set; }
        public string Database { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }

        public Connection()
        {
            StreamReader reader = new StreamReader(Application.StartupPath + @"\Connection.bin");
            this.Server = reader.ReadLine().Split(':')[1];
            this.Database = reader.ReadLine().Split(':')[1];
            this.UserName = reader.ReadLine().Split(':')[1];
            this.Password = reader.ReadLine().Split(':')[1];
            reader.Close();
        }

        public static void WriteFile(string server, string data, string uid, string pass)
        {
            StreamWriter writer = new StreamWriter(Application.StartupPath + @"\Connection.bin");
            writer.WriteLine("Server :" + server);
            writer.WriteLine("Database :" + data);
            writer.WriteLine("UserName :" + uid);
            writer.WriteLine("PassWord :" + pass);
            writer.Close();
        }
       

        // Đọc qua lớp Helper
        public string rosv()
        {
            return this.Server.Trim();
        }
        public string rodb()
        {
            return this.Database.Trim();
        }
        public string rous()
            {
            return this.UserName.Trim();
            }
        public string rops()
        {
        return this.Password.Trim();
        }
        
        public SqlConnection GetConnect()
        {
            if (this.UserName != "")
                return new SqlConnection("Data Source=" + this.Server + ";Initial Catalog=" + this.Database + ";User Id=" + this.UserName + ";Password=" + this.Password + ";");
            else
                return new SqlConnection("Data Source=" + this.Server + ";Initial Catalog=" + this.Database + ";Integrated Security=True");
        }
    }
}
